package com.aos.erp.common.dao.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlHelper;

import java.util.List;

public class Condition {
    private QueryWrapper wrapper = new QueryWrapper();
    private Class aClass;

    public static Condition create(Class aClass){
        return new Condition(aClass);
    }

    public Condition() {
    }

    public Condition(Class aClass) {
        this.aClass = aClass;
    }

    public Condition eqColumn(String column, Object value) {
        wrapper.eq(column, value);
        return this;
    }

    public Condition eqFiled(String name, Object value) {
        eqColumn(getColumn(this.aClass, name), value);
        return this;
    }

    public Condition neColumn(String column, Object value) {
        wrapper.ne(column, value);
        return this;
    }

    public Condition neFiled(String name, Object value) {
        neColumn(getColumn(this.aClass, name), value);
        return this;
    }


    public Condition likeColumn(String column, Object value) {
        wrapper.like(column, value);
        return this;
    }

    public Condition likeFiled(String name, Object value) {
        eqColumn(getColumn(this.aClass, name), value);
        return this;
    }



    public Condition gtColumn(String column, Object value) {
        wrapper.gt(column, value);
        return this;
    }

    public Condition gtFiled(String name, Object value) {
        gtColumn(getColumn(this.aClass, name), value);
        return this;
    }

    public static String getColumn(Class t, String name) {
        TableInfo tableInfo = SqlHelper.table(t);
        if (tableInfo != null) {
            if(tableInfo.getKeyProperty().equals(name)){
                return tableInfo.getKeyColumn();
            }
            List<TableFieldInfo> list = tableInfo.getFieldList();
            for (TableFieldInfo fieldInfo : list) {
                if (fieldInfo.getProperty().equals(name)) {
                    return fieldInfo.getColumn();
                }
            }
        }
        return "";
    }

    public Condition orderByAsc(String filed){
        wrapper.orderByAsc(getColumn(this.aClass,filed));
        return this;
    }


    public Condition orderByDesc(String filed){
        wrapper.orderByDesc(getColumn(this.aClass,filed));

        return this;
    }

    public Condition size(long size){
        wrapper.last("LIMIT "+size);
        return this;
    }


    /**
     * 小于 < less than
     * @param filed
     * @param value
     * @return
     */
    public Condition lt(String filed,Object value){
        wrapper.lt(getColumn(this.aClass,filed),value);
        return this;
    }



    /**
     * 小于等于 <= less equals
     * @param filed
     * @param value
     * @return
     */
    public Condition le(String filed,Object value){
        wrapper.le(getColumn(this.aClass,filed),value);
        return this;
    }



    /**
     * 大于 >
     * @param filed
     * @param value
     * @return
     */
    public Condition gt(String filed,Object value){
        wrapper.gt(getColumn(this.aClass,filed),value);

        return this;
    }



    /**
     * 大于等于 >=
     * @param filed
     * @param value
     * @return
     */
    public Condition ge(String filed,Object value){
        wrapper.ge(getColumn(this.aClass,filed),value);
        return this;
    }


    public QueryWrapper getWrapper() {
        return wrapper;
    }


}
